<template>
  <div class="transaction-maintenance-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>交易类别维护</span>
          <el-button type="primary" @click="handleAdd">新增交易类别</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="类别编码">
          <el-input v-model="queryForm.code" placeholder="请输入类别编码" />
        </el-form-item>
        <el-form-item label="类别名称">
          <el-input v-model="queryForm.name" placeholder="请输入类别名称" />
        </el-form-item>
        <el-form-item label="业务类型">
          <el-select
            v-model="queryForm.businessType"
            placeholder="请选择业务类型"
          >
            <el-option label="采购" value="purchase" />
            <el-option label="销售" value="sale" />
            <el-option label="生产" value="production" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="code" label="类别编码" />
        <el-table-column prop="name" label="类别名称" />
        <el-table-column prop="businessType" label="业务类型">
          <template #default="scope">
            {{ businessTypes[scope.row.businessType] }}
          </template>
        </el-table-column>
        <el-table-column
          prop="description"
          label="说明"
          show-overflow-tooltip
        />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag
              :type="scope.row.status === 'active' ? 'success' : 'danger'"
            >
              {{ scope.row.status === "active" ? "启用" : "停用" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button link type="primary" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              link
              :type="scope.row.status === 'active' ? 'danger' : 'success'"
              @click="handleToggleStatus(scope.row)"
            >
              {{ scope.row.status === "active" ? "停用" : "启用" }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 交易类别表单对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增交易类别' : '编辑交易类别'"
      width="500px"
    >
      <el-form
        :model="transactionForm"
        label-width="100px"
        :rules="rules"
        ref="transactionFormRef"
      >
        <el-form-item label="类别编码" prop="code">
          <el-input
            v-model="transactionForm.code"
            :disabled="dialogType === 'edit'"
          />
        </el-form-item>
        <el-form-item label="类别名称" prop="name">
          <el-input v-model="transactionForm.name" />
        </el-form-item>
        <el-form-item label="业务类型" prop="businessType">
          <el-select v-model="transactionForm.businessType">
            <el-option label="采购" value="purchase" />
            <el-option label="销售" value="sale" />
            <el-option label="生产" value="production" />
            <el-option label="其他" value="other" />
          </el-select>
        </el-form-item>
        <el-form-item label="说明">
          <el-input v-model="transactionForm.description" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

const queryForm = reactive({
  code: "",
  name: "",
  businessType: "",
});

const businessTypes = {
  purchase: "采购",
  sale: "销售",
  production: "生产",
  other: "其他",
};

const tableData = ref([
  {
    code: "T001",
    name: "普通采购",
    businessType: "purchase",
    description: "日常原材料采购",
    status: "active",
  },
]);

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(100);

const dialogVisible = ref(false);
const dialogType = ref("add");
const transactionForm = reactive({
  code: "",
  name: "",
  businessType: "",
  description: "",
});

const rules = {
  code: [{ required: true, message: "请输入类别编码", trigger: "blur" }],
  name: [{ required: true, message: "请输入类别名称", trigger: "blur" }],
  businessType: [
    { required: true, message: "请选择业务类型", trigger: "change" },
  ],
};

// 查询方法
const handleSearch = () => {
  // 实现查询逻辑
};

const handleReset = () => {
  queryForm.code = "";
  queryForm.name = "";
  queryForm.businessType = "";
};

// 表单操作方法
const handleAdd = () => {
  dialogType.value = "add";
  dialogVisible.value = true;
  Object.assign(transactionForm, {
    code: "",
    name: "",
    businessType: "",
    description: "",
  });
};

const handleEdit = (row) => {
  dialogType.value = "edit";
  dialogVisible.value = true;
  Object.assign(transactionForm, row);
};

const handleSubmit = () => {
  // 实现提交逻辑
  ElMessage.success(dialogType.value === "add" ? "新增成功" : "编辑成功");
  dialogVisible.value = false;
};

const handleToggleStatus = (row) => {
  const action = row.status === "active" ? "停用" : "启用";
  ElMessageBox.confirm(`确认${action}该交易类别吗？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    row.status = row.status === "active" ? "inactive" : "active";
    ElMessage.success(`${action}成功`);
  });
};

// 分页方法
const handleSizeChange = (val) => {
  pageSize.value = val;
  // 重新加载数据
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
  // 重新加载数据
};
</script>

<style scoped>
.transaction-maintenance-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
